import qiniu

# 文件地址前缀: http://oyucyko3w.bkt.clouddn.com/
# oyucyko3w.bkt.clouddn.com/FoUE421iwqa7dd7_4ft4c8kSX7xZ
# oyucyko3w.bkt.clouddn.com/FoUE421iwqa7dd7_4ft4c8kSX7xZ
access_key = 'yV4GmNBLOgQK-1Sn3o4jktGLFdFSrlywR2C-hvsW'
secret_key = 'bixMURPL6tHjrb8QKVg2tm7n9k8C7vaOeQ4MEoeW'
bucket_name = 'ihome'


def upload(file_bytes):
    """
    上传文件到七牛云
    :param data: 要上传的文件的二进制
    """
    # 根据access_key, secret_key 创建对象
    q = qiniu.Auth(access_key, secret_key)
    # 指定自己的存储地址bucket_name = ihome
    token = q.upload_token(bucket_name)
    # 将二进制文件上传,返回ret字典, info对象
    ret, info = qiniu.put_data(token, None, file_bytes)

    print(ret["key"])

    if info.status_code != 200:
        raise Exception("七牛云上传失败")
    return ret["key"]

# if __name__ == '__main__':
#     path = "/home/python/Desktop/gaoda.jpg"
#     with open(path, "rb") as file:
#         upload(file.read())
        # print("key: ", result)